package com.sunrate.investingquote.service.router.comparator;

import com.sunrate.investingquote.bean.router.ChannelPriorityConfig;
import com.sunrate.investingquote.bean.router.ChannelRouter;
import com.sunrate.investingquote.dto.router.comparator.RouterSortContext;
import com.sunrate.investingquote.enums.RouterPriorityEnum;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

/**
 * @author yyh
 * @description:
 * @date 2023/06/29/ 17:05
 */
public class RouterComparatorContext {

    private RouterComparatorContext() {
        throw new IllegalStateException("RouterComparatorContext class create error");
    }

    public static Comparator<RouterSortContext<ChannelRouter>> create(List<ChannelPriorityConfig> configs) {
        Comparator<RouterSortContext<ChannelRouter>> comparator = Comparator.comparing(demo -> 1);
        for (ChannelPriorityConfig config : configs) {
            if (config.getCode().equals(RouterPriorityEnum.FEE_RATE.getCode())) {
                comparator = comparator.thenComparing(RouterSortContext::getFeeRate, Comparator.nullsLast(BigDecimal::compareTo));
            }
            if (config.getCode().equals(RouterPriorityEnum.COST_TIME.getCode())) {
                comparator = comparator.thenComparing(RouterSortContext::getCostTime, Comparator.nullsLast(Integer::compareTo));
            }
        }
        return comparator.thenComparing(RouterSortContext::getDefaultPriority, Comparator.nullsLast(Integer::compareTo).reversed());
    }
}
